Skip to content

Add C0 unit test for UsernameValidation#7

Open
namnhp-2137 wants to merge 1 commit intosun7pro:masterfrom
namnhp-2137:C0_unit_test_for_username_valid
Open

Add C0 unit test for UsernameValidation#7
namnhp-2137 wants to merge 1 commit intosun7pro:masterfrom
namnhp-2137:C0_unit_test_for_username_valid

Conversation

@namnhp-2137
Copy link
Copy Markdown

@namnhp-2137 namnhp-2137 commented Jul 16, 2020

Purpose/Notes (*)

C0: Unit testt for class UsernameValidation
#3

Coverage (*)

(PHPUnit coverage screenshot)
image

Checklist (*)

  • Pull request has been self-reviewed
  • Pull request has a meaningful description of its purpose
  • Pull request has screenshot of phpunit coverage

@namnhp-2137 namnhp-2137 force-pushed the C0_unit_test_for_username_valid branch from 5f71006 to 2c994d3 Compare July 16, 2020 07:11
{
// TODO: write 6 tests to coverage 100% code
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)))), 1, $length);
Copy link
Copy Markdown
Member

@tuanpt-0634 tuanpt-0634 Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Có cần phải hàm phức tạp này không em? Viết sao cho nó vừa đúng case, vừa đơn giản, vừa đảm bảo chạy nhanh

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Em thấy testcase nó cũng như class bình thường thôi ạ, viết sao cho nó thoải mái + reuse cao là được. Ngoài ra nữa thì hàm này sẽ suppỏt cho 1 case là nếu anh change MaxLength và MinLength thì nó vẫn đảm bảo là test case vẫn đúng. Ở đây em ko muốn người ta phải tập trung vào cách define hoặc config mà tập chung vào test thì nó sẽ oke hơn

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nếu anh viết test case như thế này thì có đơn giản hơn không :D

public function test_IT_not_pass_WHEN_value_exceed_maxlength() 
{
    $username = str_pad('a', UsernameValidation::MAX_LENGTH + 1, 'b');
    $validation = new UsernameValidation;

    $this->assertFalse($validation->isValid($username));
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str_pad('a', UsernameValidation::MAX_LENGTH + 1, 'b');

Oke a, cái này em giờ mới biết dùng được kiểu này :v
để e sửa ạ

$this->assertEquals('Maximum length is ' . UsernameValidation::MAX_LENGTH, $usernameValidation->getMessage());
}

public function test_username_have_dash_at_begin_or_end_then_false_and_show_message()
Copy link
Copy Markdown
Member

@tuanpt-0634 tuanpt-0634 Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So với cách đặt tên:

test_IT_not_pass_WHEN_value_has_dash_at_begining

Thì có dễ hiểu hơn không nhỉ? Ở đây IT là class under test => it not pass nghĩa là usernam validation not pass WHEN là điều kiện đầu vào

Khi dùng --testdox ta sẽ có message:

Username Validation (Tests\C0\UsernameValidation)
 ✔ IT not pass WHEN value exceed maxlength
 ✔ IT not pass WHEN value has dash at begining
 ✔ IT not pass WHEN value has multiple dashes

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuanpt-0634
VIết như anh cũng được, nói chung tùy vào ngữ cảnh. Nhưng trên thực tế thì sử dụng từ pass hoặc từ it thì nó khá là ít ngữ cảnh để dùng và thực ra nó khá là chung chung. Cái ở đây em muốn mọi người nhìn vào có thể hiểu ngay lập tức function này test gì chứ ko phải là nhìn vào function xong nhìn thêm vào class nữa vì trong trường hợp 1 class nhiều case thì giả sử nhé số case xuống dòng vượt quá chiều cao của màn hình => anh phải khéo lên tìm cái class của nó để xem nó đang test cái gì...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass ở đây dùng cho class validation thôi, vì validation thì chỉ có pass hay failed. Còn anh đang muốn suggest chung 1 pattern đặt tên cho test, ko bắt buộc nhưng dễ follow lúc đầu. Ở đây anh nghĩ nên xem class là unit thay vì từng method, khi đó mình sẽ test hành vi của class thay vì test từng method, chẳng hạn có thay đổi tên method nhưng có thể không cần đặt lại tên test case, anh đúc kết từ bài này https://dzone.com/articles/7-popular-unit-test-naming

Should_ExpectedBehavior_When_StateUnderTest() {}
Should_ThrowException_When_AgeLessThan18() {}
Should_FailToWithdrawMoney_ForInvalidAccount() {}
Should_FailToAdmit_IfMandatoryFieldsAreMissing() {}

=>

public function test_it_expected_behavior_when_condition() {}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oki anh, thế thì mình nên tổng hợp lại rồi để đó là 1 convention ạ . Kiểu thế thì mọi người cũng dễ follow hơn

Copy link
Copy Markdown
Author

@namnhp-2137 namnhp-2137 Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anw nếu mọi người quen rồi thì đặt như thế nào cũng được mà a :))), Freestyle mà ✌️
Miễn sao nó thể hiện được tính chất là test gì ở đâu và kết quả như nào là ổn áp ạ :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants